package com.easyj.base.storage.service;

import org.springframework.web.multipart.MultipartFile;

import com.easyj.base.common.storage.PathFormat;
import com.easyj.base.exception.EasyjException;
import com.easyj.base.exception.FileStoreException;

public class StorageConstants {


	//普通附件
	public static final String AppendDir="append"; //保存目录名
	public static final long  AppendFileMaxSize=10*1024*1024;
	
	//图片文件
	public static final String ImageDir="image";
	public static final long  ImageFileMaxSize=10*1024*1024;
	public static  String[] ImageFileTypes= {".JPG",".JPEG",".PNG",".GIF",".BMP"};
	
	//头像文件
	public static  final String AvatarDir="avatar";
	public static final long AvatarFileMaxSize=4*1024*1024;	
	public static  String[] AvatarFileTypes= {".JPG",".JPEG",".PNG",".GIF"}; 
	
	//excel导入结果
	public static final String ImportResultDir="importResult";
	
	
	
	public static boolean containType(String type,String[] types) {
		type=type.toUpperCase();
		for(String t:types) {
			if(t.equals(type)) {
				return true;
			}
		}
		return false;
	}
	
	public static void CheckAppendFile(MultipartFile file) throws FileStoreException {
		String originFileName=file.getOriginalFilename();
		String suffix = PathFormat.getSuffixByFilename(originFileName);
		suffix=suffix.toUpperCase();
		if(suffix.equals(".EXE")||suffix.equals(".BAT")||suffix.equals(".JS")||suffix.equals(".HTM")||suffix.equals(".HTML")) {
			throw new FileStoreException(EasyjException.FileTypeError,"不是允许上传exe、bat、js、html文件！");
		}
		//check file size
		if (file.getSize() > StorageConstants.AppendFileMaxSize) {  
	   		 throw new FileStoreException(EasyjException.OverMaxSize,"附件太大了，超出了10M。"); 
	   	}
		
	}
	
	public static void checkImageFile(MultipartFile file) throws FileStoreException {
		//check文件类型
		String originFileName=file.getOriginalFilename();
		String suffix = PathFormat.getSuffixByFilename(originFileName);
		if(!containType(suffix,StorageConstants.ImageFileTypes)) {
			throw new FileStoreException(EasyjException.FileTypeError,"不是允许的文件类型！");
		}
		//check file size
		if (file.getSize() > StorageConstants.ImageFileMaxSize) {  
			 throw new FileStoreException(EasyjException.OverMaxSize,"头像件太大了，超出了10M。"); 
		    }
	}	
	public static void checkAvatarFile(MultipartFile file) throws FileStoreException {
		//check文件类型
		String originFileName=file.getOriginalFilename();
		String suffix = PathFormat.getSuffixByFilename(originFileName);
		if(!containType(suffix,StorageConstants.AvatarFileTypes)) {
			throw new FileStoreException(EasyjException.FileTypeError,"不是允许的文件类型！");
		}
		//check file size
		if (file.getSize() > StorageConstants.AvatarFileMaxSize) {  
			 throw new FileStoreException(EasyjException.OverMaxSize,"头像件太大了，超出了4M。"); 
		    }
	}	
}
